<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>{{ i18n('organization.member.from') }}</div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button icon="Back" @click="router.go(-1)">
              {{ i18n('common.return') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_form_main">
        <el-form label-width="120px" ref="formRef" :model="dataForm" :rules="dataRule">
          <el-card class="box-card" shadow="never">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('common.basic_info') }}</span>
              </div>
            </template>
            <div class="text item">
              <el-form-item :label="i18n('member.username')" prop="username">
                <el-input :placeholder="i18n('common.please_input') + i18n('organization.member.mobile')" v-model="dataForm.username" @change="getMemberData"></el-input>
              </el-form-item>

              <el-form-item :label="i18n('organization.member.role_id')" prop="role_id">
                <el-select v-model="dataForm.role_id" :placeholder="i18n('common.please_select') + i18n('organization.member.role_id')" clearable filterable>
                  <el-option v-for="(v, k) in roleList" :label="v.title" :key="k" :value="v.id">
                  </el-option>
                </el-select>
              </el-form-item>
            </div>
          </el-card>

          <el-card class="box-card mt10" shadow="never" v-if="memberVisit">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('member.info') }}</span>
              </div>
            </template>
            <div class="text item">
              <el-row>
                <el-col :span="3" class="text-center">
                  <el-image class="logo" :src="dataForm.member.avatar" :lazy="true">
                    <template v-slot:error>
                      <div class="image-slot">
                        <el-icon><Picture /></el-icon>
                      </div>
                    </template>
                  </el-image>
                </el-col>
                <el-col :span="21">
                  <el-descriptions border>
                    <el-descriptions-item width="200" :label="i18n('organization.chinese_title')" label-align="right">
                      <el-text v-if="dataForm.member  && dataForm.member.organization">
                        {{ dataForm.member.organization.chinese_title }}
                      </el-text>
                    </el-descriptions-item>
                    <el-descriptions-item width="200" :label="i18n('organization.english_title')" label-align="right" :span="2">
                      <el-text v-if="dataForm.member  && dataForm.member.organization">
                        {{ dataForm.member.organization.english_title }}
                      </el-text>
                    </el-descriptions-item>

                    <!-- 第一行 -->
                    <el-descriptions-item width="200" :label="i18n('member.username')" label-align="right" :span="3">
                      {{ dataForm.member.username }}
                    </el-descriptions-item>

                    <!-- 第二行 -->
                    <el-descriptions-item width="200" :label="i18n('member.chinese_nickname')" label-align="right">
                      {{ dataForm.member.chinese_nickname }}
                    </el-descriptions-item>
                    <el-descriptions-item width="200" :label="i18n('member.english_nickname')" label-align="right">
                      {{ dataForm.member.english_nickname }}
                    </el-descriptions-item>
                    <el-descriptions-item width="200" :label="i18n('member.role_title')" label-align="right">
                      <el-text v-if="dataForm.member  && dataForm.member.role">
                        {{ dataForm.member.role.title }}
                      </el-text>
                    </el-descriptions-item>

                    <!-- 第三行 -->
                    <el-descriptions-item width="200" :label="i18n('member.chinese_mobile')" label-align="right">
                      {{ dataForm.member.chinese_mobile }}
                    </el-descriptions-item>
                    <el-descriptions-item width="200" :label="i18n('member.english_mobile')" label-align="right">
                      {{ dataForm.member.english_mobile }}
                    </el-descriptions-item>
                    <el-descriptions-item width="200" :label="i18n('member.market_status')" label-align="right">
                      {{ dataForm.member.market_status.text }}
                    </el-descriptions-item>

                    <!-- 第四行 -->
                    <el-descriptions-item width="200" :label="i18n('member.contract.valid_time')" label-align="right">
                      <el-text v-if="dataForm.member  && dataForm.member.contract">
                        {{ dataForm.member.contract.valid_time_text  }}
                      </el-text>
                    </el-descriptions-item>
                    <el-descriptions-item width="200" :label="i18n('member.last_login_time')" label-align="right">
                      {{ dataForm.member.last_login_time }}
                    </el-descriptions-item>
                    <el-descriptions-item width="200" :label="i18n('common.create_time')" label-align="right">
                      {{ dataForm.member.create_time }}
                    </el-descriptions-item>
                  </el-descriptions>
                </el-col>
              </el-row>
            </div>
          </el-card>

          <el-card class="box-card mt10" shadow="never">
            <div class="text item">
              <el-form-item>
                <el-button
                  v-if="isAuth('module:organization:member:handle')"
                  type="primary"
                  @click="dataFormSubmit(formRef)">
                  {{ i18n('common.confirm') }}
                </el-button>
                <el-button @click="resetForm(formRef)">
                  {{ i18n('common.reset') }}
                </el-button>
              </el-form-item>
            </div>
          </el-card>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup name="organization-member-form">
  import { useBase } from "@/hooks/base/useBase"
  import { useForm } from "@/hooks/form/useForm"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('organization/member')

  const roleList = ref([])
  const memberVisit = ref(false)

  const dataForm = reactive({
    id: 0,
    organization_id: '',
    role_id: 1,
    username: '',
    member: []
  })

  const dataRule = reactive({
    role_id: [{
      required: true,
      message: i18n('organization.member.rules.role_id.require'),
      trigger: 'blur',
    }],
    username: [{
      required: true,
      message: i18n('organization.member.rules.mobile.require'),
      trigger: 'blur',
    }],
  })

  const {
    formRef,
    dataFormSubmit,
    resetForm
  } = useForm(model.value, dataForm)

  const init = (form) => {
    dataForm.id = query.id || 0

    nextTick(() => {
      form.value.resetFields()
      if (dataForm.id) {
        $http({
          url: $http.adornUrl(`/${model.value}/view/${dataForm.id}`),
          method: 'get',
          params: $http.adornParams({}),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            dataForm.organization_id = data.data.organization_id
          }
        })
      }
    })
  }

  const loadRoleList = () => {
    $http({
      url: $http.adornUrl('/organization/role/select'),
      method: 'get',
    }).then(({ data }) => {
      if (data && data.status === 200) {
        roleList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  /**
   * 获取会员信息
   */
  const getMemberData = (mobile) => {
    if(11 == mobile.length) {
      $http({
        url: $http.adornUrl(`/member/data`),
        method: 'get',
        params: $http.adornParams({
          username: mobile
        }),
      }).then(({ data }) => {
        if (data && data.data && data.status === 200) {
          memberVisit.value = true
          dataForm.member = data.data
        } else {
          memberVisit.value = false
          dataForm.member = []
        }
      })
    }
  }

  if(query.organization_id) {
    dataForm.organization_id = parseInt(query.organization_id)
  }

  onMounted(() => {
    init(formRef)

    loadRoleList()
  })
</script>

<style lang="scss" scoped>
  .logo {
    width: 6vw;
    height: 6vw;
    min-width: 120px;
    min-height: 120px;
  }
</style>
